import {Component, OnInit, OnDestroy} from '@angular/core';
import {Router} from '@angular/router';

import {HomeService} from './home.service';
import {ConfigService} from '@kylin/config';
import {RouterService} from '@kylin/router';
import {NzModalRef, NzModalService} from 'ng-zorro-antd';

@Component({
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.less']
})

export class HomeComponent implements OnInit, OnDestroy {

    sidebarMenuList: Array<any>;
    avatar = '../../assets/img/avatar.png';
    imgHostname = this.httpConfigService.imgHostname;
    dropDownVisible: boolean; // 下拉菜单状态,true为打开
    nzWidth = 200;
    nzInlineCollapsed = false;
    confirmModal: NzModalRef; // For testing by now
    notifications: any[];

    constructor(
        public homeService: HomeService,
        private httpConfigService: ConfigService,
        private router: Router,
        private routerService: RouterService,
        private modal: NzModalService
    ) {

    }

    ngOnInit() {
        this.sidebarMenuList = this.homeService.getSidebarMenuData();
        if (this.sidebarMenuList) {
            // 初始化sider状态,哪个菜单应该打开,哪个li应该有选中状态
            this.sidebarMenuList.forEach((value, index) => {
                this.sidebarMenuList[index].isOpen = false;
                value.childMenu.forEach(children => {
                    if (children.childMenu.length > 0) {
                        children.childMenu.forEach(i => {
                            if (this.router.url.indexOf(i.url) !== -1) {
                                this.sidebarMenuList[index].isOpen = true;
                                children.isOpen = true;
                                i.status = true;
                            }
                        });
                    } else {
                        if (this.router.url.indexOf(children.url) !== -1) {
                            this.sidebarMenuList[index].isOpen = true;
                            children.status = true;
                        }
                    }
                });
            });
        }
    }

    ngOnDestroy() {
        this.homeService.clearMsgDataInterval();
    }

    showConfirm(): void {
        this.confirmModal = this.modal.confirm({
            nzTitle: '退出确认',
            nzContent: '确定要退出登陆吗?',
            nzOnOk: () => {
                this.logout();
            }
        });
    }

    /**
     * 路由跳转
     * @param {string} path
     */
    public skip(path: string): void {
        this.routerService.navigate(path);
    }

    /**
     * 退出登陆
     */
    public logout(): void {
        this.homeService.logout();
    }

    /**
     * 实现sider手风琴模式
     * @param {string} menuName
     */
    nzOpenChange(menuName: string): void {
        this.sidebarMenuList.forEach((value) => {
            value.isOpen = false;
            value.name === menuName ? value.isOpen = true : value.isOpen = false;
        });
    }
}
